home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 September
/
PCWorld_2006-09_cd.bin
/
v cisle
/
hexer
/
mpth_17.exe
/
{app}
/
scripts
/
Structure Viewer
/
exe.mps
< prev
next >
Wrap
Text File
|
2006-03-19
|
11KB
|
355 lines
option title, "Windows executable (.exe, .dll ...) structure"
=
=
= Tiny Hexer script for .EXE structure view in
= mirkes.de's tiny hex editor
=
= (C) markus stephany, tinyhexer[at]mirkes[dot]de
=
INCLUDE 'def.mps'
option GlobalVars, 1
option ReadTags, 1
option target, structureviewer
var d1 dword d2 dword
var importrva dword importptr dword
var exportrva dword exportptr dword
var secstart dword numsect word
= "array" of section va, datasize and raw pointer
var sect_va text sect_ds text sect_rp text
= open active editor and goto current position/0
var editor file
editor = fileopen('::current')
if ((param_count > 0) and (dword(params(0))==1))
fileseek editor, 0
else
fileseek editor, filegetprop(editor, 'selstart')
endif
var start dword
start = filegetprop(editor, 'position')
= open browser window
var browser file
browser = fileopen('::browser', 'c')
filesetprop browser, 'accepttags', 1
filewrite browser "<font color=",'"',"blue",'"',"><b><u>Portable Executable structure:</u></b></font>\n\n"
filesetprop browser, 'accepttags', 0
filewrite browser "File: '",filegetprop(editor, 'filename'),"'\nPosition: ",start,"\n\n"
= read the EXE header
var _word word _dword dword _text text ipos qword
ipos = filepos(editor)
fileread editor _word
if _word != 0x5a4d
error 'Unknown file format'
endif
= print dos header
filewrite browser "struct _IMAGE_DOS_HEADER {\n"
_text = data2text(_word)
copytags _text _word
filewrite browser " WORD\te_magic\t\t'",_text,"'\t(",_word,")\n"
fileread editor _word
filewrite browser " WORD\te_cblp\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_cp\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_crlc\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_cparhdr\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_minalloc\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_maxalloc\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_ss\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_sp\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_csum\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_ip\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_cs\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_lfarlc\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_ovno\t\t",_word,"\n"
filewrite browser " WORD\te_res[4]\t":call WORDOUT 4:filewrite browser "\n"
fileread editor _word
filewrite browser " WORD\te_oemid\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\te_oeminfo\t",_word,"\n"
filewrite browser " WORD\te_res2[10]\t":call WORDOUT 10:filewrite browser "\n"
fileread editor _dword
filewrite browser " LONG\te_lfanew\t",_dword,"\n}\n\n"
fileseek editor, (ipos + _dword)
filewrite browser "Position: ",filepos(editor),"\n"
fileread editor _dword
if _dword != 0x4550
error 'File is not in PE format'
endif
= print magic and file header
_text = data2text(word(_dword))
copytags _text _dword
filewrite browser "DWORD\tmagic\t'",_text,"'\t(",_dword,")\n\n"
var wsopt word
filewrite browser "struct _IMAGE_FILE_HEADER {\n"
fileread editor _word
filewrite browser " WORD\tMachine\t\t\t",_word,"\n"
fileread editor numsect
filewrite browser " WORD\tNumberOfSections\t",numsect,"\n"
fileread editor _dword
filewrite browser " DWORD\tTimeDateStamp\t\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tPointerToSymbolTable\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tNumberOfSymbols\t\t",_dword,"\n"
fileread editor wsopt
filewrite browser " WORD\tSizeOfOptionalHeader\t",wsopt,"\n"
fileread editor _word
filewrite browser " WORD\tCharacteristics\t\t",_word,"\n}\n\n"
if wsopt > 0
var _byte byte rva dword
filewrite browser "struct _IMAGE_OPTIONAL_HEADER {\n"
fileread editor _word
filewrite browser " WORD\tMagic\t\t\t",_word,"\n"
fileread editor _byte
filewrite browser " BYTE\tMajorLinkerVersion\t",_byte,"\n"
fileread editor _byte
filewrite browser " BYTE\tMinorLinkerVersion\t",_byte,"\n"
fileread editor _dword
filewrite browser " DWORD\tSizeOfCode\t\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tSizeOfInitializedData\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tSizeOfUninitializedData\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tAddressOfEntryPoint\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tBaseOfCode\t\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tBaseOfData\t\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tImageBase\t\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tSectionAlignment\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tFileAlignment\t\t",_dword,"\n"
fileread editor _word
filewrite browser " WORD\tMajorOSVersion\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\tMinorOSVersion\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\tMajorImageVersion\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\tMinorImageVersion\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\tMajorSubsystemVersion\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\tMinorSubsystemVersion\t",_word,"\n"
fileread editor _dword
filewrite browser " DWORD\tReserved1\t\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tSizeOfImage\t\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tSizeOfHeaders\t\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tCheckSum\t\t",_dword,"\n"
fileread editor _word
filewrite browser " WORD\tSubsystem\t\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\tDllCharacteristics\t",_word,"\n"
fileread editor _dword
filewrite browser " DWORD\tSizeOfStackReserve\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tSizeOfStackCommit\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tSizeOfHeapReserve\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tSizeOfHeapCommit\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tLoaderFlags\t\t",_dword,"\n"
fileread editor rva
filewrite browser " DWORD\tNumberOfRvaAndSizes\t",rva,"\n"
filewrite browser " IMAGE_DATA_DIRECTORY DataDirectory[",rva,"] {\n"
call data_dir
exportrva = d1:= first entry: pointer to export table rva
call data_dir
importrva = d1:= second entry: pointer to import table rva
loop data_dir, (rva-2)
filewrite browser " }\n"
filewrite browser "}\n\n"
endif
secstart = filepos(editor)
loop section, numsect
= check export table
call rvatoabs exportrva:pop exportptr
if (exportptr != 0)
tagvar exportptr, exportptr, 0
filewrite browser "\nexport table at ",exportptr,"\n"
endif
= check import table
call rvatoabs importrva:pop importptr
if (importptr != 0)
fileseek editor importptr
tagvar importptr, importptr, 0
filewrite browser "\n================================================================\n"
filewrite browser "import table at ",importptr,"\n\n"
var i1 dword i2 dword i3 dword i4 dword i5 dword p_i dword i_ dword t_ text pi1 dword
var i6 dword
repeat
fileread editor i1
fileread editor i2
fileread editor i3
fileread editor i4
fileread editor i5
if i1 or i2 or i3 or i4 or i5
p_i = filepos(editor)
filewrite browser "struct IMAGE_IMPORT_DESCRIPTOR {\n"
filewrite browser " DWORD\tOriginalFirstThunk\t",i1,"\n"
filewrite browser " DWORD\tTimeDateStamp\t\t",i2,"\n"
filewrite browser " DWORD\tForwarderChain\t\t",i3,"\n"
call rvatoabs i4: pop i_
fileseek editor i_
fileread editor t_
tagvar t_, i_, textlen(t_)
filewrite browser " DWORD\tName\t\t\t",i4,"\t'",t_,"'\n"
filewrite browser " DWORD\tFirstThunk\t\t",i5,"\n"
filewrite browser "}\n"
if i1 == 0
call rvatoabs i5
else
call rvatoabs i1
endif
pop i_
tagvar i_, i_, 0
filewrite browser "Imported functions (IMAGE_THUNK_DATA records at ",i_,"):\n"
= msgbox text(i5)+'/'+text(i_)
fileseek editor i_
repeat
fileread editor i6
pi1 = filepos(editor)
if i6
if (i6 >= 0x8000000)
var n1 none
copytags n1 i6
i6 = i6 and 0x0fffffff
copytags i6 n1
filewrite browser " Ordinal: ",i6,"\n"
else
call rvatoabs i6:pop i_
fileseek editor i_
var hint word
fileread editor hint
fileread editor t_
filewrite browser " WORD Hint: ",hint,"\t\tName: '",t_,"'\n"
fileseek editor pi1
endif
endif
until not i6
fileseek editor p_i
filewrite browser "\n\n"
endif
until not (i1 or i2 or i3 or i4 or i5)
endif
fileclose browser
fileclose editor
end
@@data_dir:= print a data directory
fileread editor d1
fileread editor d2
filewrite browser "\t(DWORD VirtualAddress ",d1,", DWORD Size ",d2,")\n"
return
@@section:= output a section
filewrite browser "\nPosition: ", filepos(editor),"\n"
filewrite browser "================================================================\n\n"
var sectn text ds1 dword
fileread editor sectn 8
filewrite browser "struct _IMAGE_SECTION_HEADER {\n"
filewrite browser " char\tName[8]\t\t\t'",sectn,"'\n"
fileread editor _dword
filewrite browser " DWORD\tVirtualSize\t\t",_dword,"\n"
fileread editor _dword
concat sect_va data2text(_dword)
filewrite browser " DWORD\tVirtualAddress\t\t",_dword,"\n"
fileread editor ds1
concat sect_ds data2text(ds1)
filewrite browser " DWORD\tSizeOfRawData\t\t",ds1,"\n"
fileread editor d2
concat sect_rp data2text(d2)
sectn = "PointerToRawData"
tagvar sectn, (d2 + ipos),ds1
filewrite browser " DWORD\t",sectn,"\t",d2,"\n"
fileread editor _dword
filewrite browser " DWORD\tPointerToRelocations\t",_dword,"\n"
fileread editor _dword
filewrite browser " DWORD\tPointerToLinenumbers\t",_dword,"\n"
fileread editor _word
filewrite browser " WORD\tNumberOfRelocations\t",_word,"\n"
fileread editor _word
filewrite browser " WORD\tNumberOfLinenumbers\t",_word,"\n"
fileread editor _dword
filewrite browser " DWORD\tCharacteristics\t\t",_dword,"\n}\n"
return
@@WORDOUT:= output a word
var w word w1 word
pop w
repeat
fileread editor w1
filewrite browser w1
inc w -1
if w > 0
filewrite browser ", "
endif
until w == 0
return
@@rvatoabs:= convert an rva to an absolute address
= va in sect_va
= ds in sect_ds
= rp in sect_rp
var r_va dword r_ds dword r_rp dword
var r_i dword
var r_val dword
r_i = 0
pop r_val
if numsect
repeat
r_va = text2data(sect_va, r_i, 4)
r_ds = text2data(sect_ds, r_i, 4)
r_rp = text2data(sect_rp, r_i, 4)
inc r_i 1
if (r_val >= r_va) and ((r_va + r_ds) >= r_val)
inc r_val, (r_rp-r_va)
goto rva_end
endif
until (r_i == numsect)
endif
@@rva_end
push r_val
return